﻿/**  版本信息模板在安装目录下，可自行修改。
* hars_healthuserbind.cs
*
* 功 能： N/A
* 类 名： hars_healthuserbind
*
* Ver    变更日期             负责人  变更内容
* ───────────────────────────────────
* V0.01  2018/1/20 10:15:56   N/A    初版
*
* Copyright (c) 2012 Maticsoft Corporation. All rights reserved.
*┌──────────────────────────────────┐
*│　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*│　版权所有：动软卓越（北京）科技有限公司　　　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using PreeRegisterModel;

namespace PreeRegisterDAL
{
    /// <summary>
    /// 数据访问类:hars_healthuserbind
    /// </summary>
    public partial class hars_healthuserbind
    {
        public hars_healthuserbind()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperMySQL.GetMaxID("id", "hars_healthuserbind");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from hars_healthuserbind");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@id", MySqlDbType.Int32)
            };
            parameters[0].Value = id;

            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(PreeRegisterModel.hars_healthuserbind model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into hars_healthuserbind(");
            strSql.Append("pid,bindstate,hospid,phone,userid,createdate,lastdate)");
            strSql.Append(" values (");
            strSql.Append("@pid,@bindstate,@hospid,@phone,@userid,@createdate,@lastdate)");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@pid", MySqlDbType.VarChar,200),
                    new MySqlParameter("@bindstate", MySqlDbType.Int32,2),
                    new MySqlParameter("@hospid", MySqlDbType.VarChar,200),
                    new MySqlParameter("@phone", MySqlDbType.VarChar,200),
                    new MySqlParameter("@userid", MySqlDbType.VarChar,200),
                    new MySqlParameter("@createdate", MySqlDbType.Timestamp),
                    new MySqlParameter("@lastdate", MySqlDbType.Timestamp)};
            parameters[0].Value = model.pid;
            parameters[1].Value = model.bindstate;
            parameters[2].Value = model.hospid;
            parameters[3].Value = model.phone;
            parameters[4].Value = model.userid;
            parameters[5].Value = model.createdate;
            parameters[6].Value = model.lastdate;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(PreeRegisterModel.hars_healthuserbind model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update hars_healthuserbind set ");
            strSql.Append("pid=@pid,");
            strSql.Append("bindstate=@bindstate,");
            strSql.Append("hospid=@hospid,");
            strSql.Append("phone=@phone,");
            strSql.Append("userid=@userid");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@pid", MySqlDbType.VarChar,200),
                    new MySqlParameter("@bindstate", MySqlDbType.Int32,2),
                    new MySqlParameter("@hospid", MySqlDbType.VarChar,200),
                    new MySqlParameter("@phone", MySqlDbType.VarChar,200),
                    new MySqlParameter("@userid", MySqlDbType.VarChar,200),
                    new MySqlParameter("@id", MySqlDbType.Int32,50)};
            parameters[0].Value = model.pid;
            parameters[1].Value = model.bindstate;
            parameters[2].Value = model.hospid;
            parameters[3].Value = model.phone;
            parameters[4].Value = model.userid;
            parameters[5].Value = model.id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from hars_healthuserbind ");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@id", MySqlDbType.Int32)
            };
            parameters[0].Value = id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from hars_healthuserbind ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public PreeRegisterModel.hars_healthuserbind GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,pid,bindstate,hospid,phone,userid,createdate,lastdate from hars_healthuserbind ");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@id", MySqlDbType.Int32)
            };
            parameters[0].Value = id;

            PreeRegisterModel.hars_healthuserbind model = new PreeRegisterModel.hars_healthuserbind();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public PreeRegisterModel.hars_healthuserbind DataRowToModel(DataRow row)
        {
            PreeRegisterModel.hars_healthuserbind model = new PreeRegisterModel.hars_healthuserbind();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["pid"] != null)
                {
                    model.pid = row["pid"].ToString();
                }
                if (row["bindstate"] != null && row["bindstate"].ToString() != "")
                {
                    model.bindstate = int.Parse(row["bindstate"].ToString());
                }
                if (row["hospid"] != null)
                {
                    model.hospid = row["hospid"].ToString();
                }
                if (row["phone"] != null)
                {
                    model.phone = row["phone"].ToString();
                }
                if (row["userid"] != null)
                {
                    model.userid = row["userid"].ToString();
                }
                if (row["createdate"] != null && row["createdate"].ToString() != "")
                {
                    model.createdate = DateTime.Parse(row["createdate"].ToString());
                }
                if (row["lastdate"] != null && row["lastdate"].ToString() != "")
                {
                    model.lastdate = DateTime.Parse(row["lastdate"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,pid,bindstate,hospid,phone,userid,createdate,lastdate ");
            strSql.Append(" FROM hars_healthuserbind ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM hars_healthuserbind ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperMySQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from hars_healthuserbind T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperMySQL.Query(strSql.ToString());
        }

        /*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			MySqlParameter[] parameters = {
					new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@PageSize", MySqlDbType.Int32),
					new MySqlParameter("@PageIndex", MySqlDbType.Int32),
					new MySqlParameter("@IsReCount", MySqlDbType.Bit),
					new MySqlParameter("@OrderType", MySqlDbType.Bit),
					new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
					};
			parameters[0].Value = "hars_healthuserbind";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

        #endregion  BasicMethod

        #region  ExtensionMethod

        /// <summary>
        /// 判断绑定关系是否存在
        /// </summary>
        /// <param name="hospid">医院ID</param>
        /// <param name="pid">PID</param>
        /// <param name="bindstate">绑定状态 1绑定 2解绑</param>
        /// <returns></returns>
        public bool Exists(string hospid, string pid, int bindstate)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from hars_healthuserbind");
            strSql.Append(" where hospid=@hospid and pid=@pid and bindstate=@bindstate ");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@hospid", MySqlDbType.VarChar,200),
                    new MySqlParameter("@pid", MySqlDbType.VarChar,200),
                    new MySqlParameter("@bindstate", MySqlDbType.Int32,2)
            };
            parameters[0].Value = hospid;
            parameters[0].Value = pid;
            parameters[0].Value = bindstate;
            return DbHelperMySQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 获取绑定关系
        /// </summary>
        /// <param name="hospid">医院ID</param>
        /// <param name="pid">PID</param>
        /// <returns></returns>
        public PreeRegisterModel.hars_healthuserbind GetModel(string hospid, string pid, string userid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,pid,bindstate,hospid,phone,userid,createdate,lastdate from hars_healthuserbind ");
            strSql.Append(" where hospid=@hospid and pid=@pid and userid=@userid");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@hospid", MySqlDbType.VarChar,200),
                    new MySqlParameter("@pid", MySqlDbType.VarChar,200),
                    new MySqlParameter("@userid", MySqlDbType.VarChar,200),
            };
            parameters[0].Value = hospid;
            parameters[1].Value = pid;
            parameters[2].Value = userid;

            PreeRegisterModel.hars_healthuserbind model = new PreeRegisterModel.hars_healthuserbind();
            DataSet ds = DbHelperMySQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 更新绑定状态
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <param name="bindstate">绑定状态</param>
        /// <returns></returns>
        public bool Update(int id, int bindstate)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update hars_healthuserbind set ");
            strSql.Append("bindstate=@bindstate,");
            strSql.Append("lastdate=@lastdate");
            strSql.Append(" where id=@id");
            MySqlParameter[] parameters = {
                    new MySqlParameter("@bindstate", MySqlDbType.Int32,2),
                    new MySqlParameter("@lastdate", MySqlDbType.Timestamp),
                    new MySqlParameter("@id", MySqlDbType.Int32,50)};
            parameters[0].Value = bindstate;
            parameters[1].Value = DateTime.Now;
            parameters[2].Value = id;

            int rows = DbHelperMySQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion  ExtensionMethod
    }
}

